package howl.types;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.users.User;
import compiler.HowlType;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TableDescriptor{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private  Integer PachubeFeedID;

	@Persistent
	private String Name;
	
	@Persistent
	private User Owner;

	@Persistent
	private Date creation;
	
	@Persistent
	private ArrayList<String> stringmapper;

	@Persistent
	private ArrayList<Unit> unitmapper;

	public TableDescriptor(String name) {
		super();
		this.creation = new Date();
		this.unitmapper = new ArrayList<Unit>();
		this.stringmapper = new ArrayList<String>();
		this.Name = name;
	}

	public TableDescriptor(String name, String... arg) {
		super();
		this.creation = new Date();
		this.Name = name;
		this.unitmapper = new ArrayList<Unit>();
		this.stringmapper = new ArrayList<String>();
		try {
			if ((arg.length % 2) == 0) {
				for (int i = 0; i < arg.length; i = i + 2) {
					this.stringmapper.add(arg[i]);
					this.unitmapper.add(Unit.valueof(arg[i + 1]));
				}
			} else {

			}
		} catch (IllegalArgumentException e){
			e.printStackTrace();
		}catch (Exception e) {
		
			e.printStackTrace();
		}
	}
	
	public Unit lookupType(String unit){
		for(int i = 0 ;i< this.stringmapper.size();i++){
			if(unit.equals(this.stringmapper.get(i))){
				return this.unitmapper.get(i);
			}
		}
		return null;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public Date getCreation() {
		return creation;
	}

	public void setCreation(Date creation) {
		this.creation = creation;
	}

	public ArrayList<String> getStringmapper() {
		return stringmapper;
	}

	public void setStringmapper(ArrayList<String> stringmapper) {
		this.stringmapper = stringmapper;
	}

	public ArrayList<Unit> getUnitmapper() {
		return unitmapper;
	}

	public void setUnitmapper(ArrayList<Unit> unitmapper) {
		this.unitmapper = unitmapper;
	}

	public User getOwner() {
		return Owner;
	}

	public void setOwner(User owner) {
		Owner = owner;
	}

	/**
	 * @return the pachubeFeedID
	 */
	public Integer getPachubeFeedID() {
		return PachubeFeedID;
	}

	/**
	 * @param pachubeFeedID the pachubeFeedID to set
	 */
	public void setPachubeFeedID(Integer pachubeFeedID) {
		PachubeFeedID = pachubeFeedID;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((Name == null) ? 0 : Name.hashCode());
		result = prime * result + ((Owner == null) ? 0 : Owner.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TableDescriptor other = (TableDescriptor) obj;
		if (Name == null) {
			if (other.Name != null)
				return false;
		} else if (!Name.equals(other.Name))
			return false;
		if (Owner == null) {
			if (other.Owner != null)
				return false;
		} else if (!Owner.equals(other.Owner))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
